package com.rajaev.dtu.tractor.interfaces;

import com.github.pagehelper.PageInfo;
import com.rajaev.dtu.tractor.domain.entity.UserInfo;
import com.rajaev.dtu.tractor.domain.service.UserInfoDomain;
import com.rajaev.dtu.tractor.domain.vo.ResourceVo;
import com.rajaev.dtu.tractor.domain.vo.UserVo;
import com.rajaev.dtu.tractor.infrastructure.base.errors.BusinessException;
import com.rajaev.dtu.tractor.infrastructure.helper.UserSecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author tianjie
 */
@RestController
public class UserController {

    @Autowired
    UserInfoDomain userService;

    @Autowired
    UserSecurityUtil userSecurityUtil;

    @PostMapping("/user/add")
    public void addUser(UserVo userVo) throws BusinessException {
        userService.insertUser(userVo);
    }

    @PostMapping("/user/edit")
    public void editUser(UserVo userVo) throws BusinessException {
        userService.editUser(userVo);
    }


    @PostMapping("/user/modifyLoginUserSystemDisplayName")
    public void modifyLoginUserSystemDisplayName(String systemDisplayName, HttpServletRequest request) {
        UserInfo userInfo = userSecurityUtil.getLoginUser(request).getUserInfo();
        userInfo.setSystemDisplayName(systemDisplayName);
        userService.updateUserSystemDisplayName(userInfo.getId(),systemDisplayName);
    }

    @GetMapping("/user/getList")
    public PageInfo<UserVo> selectUsers(@RequestParam(defaultValue = "10") Integer pageSize,@RequestParam(defaultValue = "1") Integer pageNum) {
        return userService.pageQueryUsers(pageSize, pageNum);
    }

    @PostMapping("/user/delete")
    public void deleteUser(Integer userId) {
        userService.deleteUser(userId);
    }

    /**
     * 重置密码
     *
     * @param userId
     */
    @PostMapping("/user/resetPassword")
    public void resetUserPassword(Integer userId) {
        userService.resetPassword(userId);
    }

    /**
     * 修改密码
     *
     * @param oldPassword
     * @param newPassword
     * @throws BusinessException
     */
    @PostMapping("/user/updatePassword")
    public void updatePassword(String oldPassword, String newPassword,HttpServletRequest request) throws BusinessException {
        UserInfo userInfo = userSecurityUtil.getLoginUser(request).getUserInfo();
        userService.updatePassword(userInfo.getId(), newPassword, oldPassword);
    }

    /**
     * 修改初始密码
     *
     * @param newPassword
     * @throws BusinessException
     */
    @PostMapping("/user/updateOriginalPassword")
    public void updateOriginalPassword(@NotNull String newPassword,HttpServletRequest request) throws BusinessException {
        UserInfo userInfo = userSecurityUtil.getLoginUser(request).getUserInfo();
        userService.updateOriginalPassword(newPassword, userInfo);
    }


    @GetMapping("/user/getResourceList")
    public List<ResourceVo> getUserRoles(HttpServletRequest request) throws BusinessException {
        UserInfo userInfo = userSecurityUtil.getLoginUser(request).getUserInfo();
        return userService.selectResource(userInfo.getId());
    }

}
